/* eslint-disable no-prototype-builtins */
/* eslint-disable no-useless-escape */
/* eslint-disable no-redeclare */
/* eslint-disable vars-on-top */
/* eslint-disable no-var */
/* eslint-disable block-scoped-var */

/**
 * @name  {[string]} url
 * @return {[string]} 获取网址参数
 */
export function getUrlParams(name) {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  const r = decodeURI(window.location.search).substr(1).match(reg)
  if (r != null) return r[2]
  return null
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"'
    + decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"')
      .replace(/\+/g, ' ')
    + '"}'
  )
}

/* 删除url指定参数，返回url */
export function delParamsUrl(url, name) {
  const baseUrl = url.split('?')[0] + '?'
  const query = url.split('?')[1]
  if (query.indexOf(name) > -1) {
    const obj = {}
    const arr = query.split('&')
    for (let i = 0; i < arr.length; i++) {
      arr[i] = arr[i].split('=')
      obj[arr[i][0]] = arr[i][1]
    }
    delete obj[name]
    var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g, '').replace(/\:/g, '=').replace(/\,/g, '&')
    return url
  }
  return url
}
/**
 * 图片加载完毕执行函数
 * @arr  {[array]}
 * @callback  {Function}
 */
export function imgLoadAll(arr, callback) {
  const arrImg = []
  for (let i = 0; i < arr.length; i++) {
    const img = new Image()
    img.src = arr[i]
    img.onload = function () {
      arrImg.push(this)
      if (arrImg.length === arr.length) {
        callback && callback()
      }
    }
  }
}
/**
 * 音频加载完毕执行函数
 * @src  {[string]}
 * @callback  {Function}
 */
export function loadAudio(src, callback) {
  const audio = new Audio(src)
  audio.onloadedmetadata = callback
  audio.src = src
}
/* DOM转字符串 */
export function domToStirng(htmlDOM) {
  const div = document.createElement('div')
  div.appendChild(htmlDOM)
  return div.innerHTML
}
/* 字符串转DOM */
export function stringToDom(htmlString) {
  const div = document.createElement('div')
  div.innerHTML = htmlString
  return div.children[0]
}

/**
 * encodeToURIString 把一个对象类型转化为问号参数类型的字符串
 * @param data 要解析的对象
 * @param isUIR 是否转化为URIComponent，默认转化，只有当值为false时，不转化
 * @returns '' 问号参数类型的字符串
 */
export const encodeToURIString = function (data, isUIR) {
  // 默认参数isUIR不为false，值皆为true
  if (isUIR !== false) {
    isUIR = true
  }
  const ary = []
  if (Object.prototype.toString.call(data) === '[object Object]') {
    for (let key in data) {
      if (data.hasOwnProperty(key)) {
        if (isUIR) {
          try {
            key = decodeURIComponent(key)
            data[key] = decodeURIComponent(data[key])
            ary.push(key + '=' + encodeURIComponent(data[key]))
          } catch (ex) {
            ary.push(key + '=' + data[key])
          }
        } else {
          ary.push(key + '=' + data[key])
        }
      }
    }
  }

  return ary.join('&')
}


/**
 * addStringToUrl 将对象转化为问号参数形式添加在url地址的后面，会进行URI编码处理
 * @param url 原url地址
 * @param param 参数对象
 * @returns '' 返回拼接好的新url地址
 */
export function addParams(url, param) {
  if (!param) {
    return url
  }
  const hasSearch = /\?/.test(url)
  const data = encodeToURIString(param)
  return url + (hasSearch ? '&' : '?') + data
}
